﻿Imports IntoNails.Model
Imports System.IO

Namespace DataAccess

    <Serializable()> _
    Public Class EmployeeRepository
        Inherits RepositoryBase(Of Employee)
        Implements IEmployeeRepository

#Region "Declarations"
        Private ReadOnly _dataFile As String
        Public ReadOnly Property DataFile() As String
            Get
                Return _dataFile
            End Get
        End Property
#End Region

#Region "Constants"
        Private Const EMPLOYEE_FILE As String = "{0}\Data\Employees.dat"
#End Region

#Region "Constructors"

        Public Sub New()
        End Sub

        Public Sub New(ByVal dataFile As String)
            Me.New()
            Me._dataFile = dataFile
            Me.Load()
        End Sub

#End Region

        Public Function Add(ByVal data As Employee) As Boolean _
            Implements IDataRepository(Of Employee).Add

            If Not Me.InnerDataList.Contains(data) Then
                Me.InnerDataList.Add(data)
                Return True
            End If

            Return False
        End Function

        Public Function GetItem(ByVal uniqueCode As Integer) As Employee _
            Implements IDataRepository(Of Employee).GetItem

            Return Me.InnerDataList.Where(Function(e) e.UniqueCode = uniqueCode).FirstOrDefault()

        End Function

        Public Function GetRawCollection() As System.Collections.Generic.List(Of Employee) _
            Implements IDataRepository(Of Employee).GetRawCollection

            Return Me.InnerDataList

        End Function

        Public Function Remove(ByVal uniqueCode As Integer) As Boolean _
            Implements IDataRepository(Of Employee).Remove

            Dim removed As Employee = Me.GetItem(uniqueCode)

            If removed IsNot Nothing Then
                removed.IsValid = False

                removed.DateChanged = Date.Today
                Return True
            End If

            Return False
        End Function

        Public Function Update(ByVal uniqueCode As Integer, ByVal newData As Employee) As Boolean _
            Implements IDataRepository(Of Employee).Update

            Dim e As Employee = Me.GetItem(uniqueCode)
            If e Is Nothing Then
                Return False
            End If

            If newData Is Nothing Or newData.UniqueCode <> e.UniqueCode Then
                Return False
            End If

            With e
                e.FullName = newData.FullName
                e.BirthDate = newData.BirthDate
                e.Address = newData.Address
                e.ContactNumber = newData.ContactNumber
                e.CellPhone = newData.CellPhone
                e.IsValid = newData.IsValid

                e.DateChanged = Date.Today
            End With

            Return True
        End Function

        Public Function GetValidEmployeeCollection() As List(Of Employee) _
            Implements IEmployeeRepository.GetValidEmployeeCollection

            Return Me.InnerDataList.Where(Function(e) e.IsValid = True).ToList()

        End Function

#Region "RepositoryBase Overriden"

        Public Overrides Sub Load()
            If IO.File.Exists(Me.DataFile) Then
                Using readStream As New FileStream(Me.DataFile, FileMode.Open, FileAccess.Read)
                    Me.InnerDataList = readStream.Deserialize(Of List(Of Employee))()
                End Using
            Else
                Me.InnerDataList = New List(Of Employee)()
            End If
        End Sub

        Public Overrides Sub Save()
            Using writeStream As New FileStream(Me.DataFile, FileMode.OpenOrCreate, FileAccess.Write)
                Me.InnerDataList.Serialize(writeStream)
            End Using
        End Sub

#End Region

    End Class
End Namespace
